<template>
  <div class='container'>
    <el-card class="box-card">
      <!-- 头部面包屑 -->
      <template #header>
        <div class="clearfix" v-if="$route.query.id">
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">面板</el-breadcrumb-item>
            <el-breadcrumb-item>{{$route.query.name}}</el-breadcrumb-item>
            <el-breadcrumb-item>标签</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
      </template>
      <div class="headers">
        <!-- 搜索表单 -->
        <el-form :inline="true" class="demo-form-inline" size="small">
          <el-form-item label="标签名称">
            <el-input v-model="query.tabName"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="query.state" placeholder="请选择">
              <el-option label="开启" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="clearSearch">清除</el-button>
            <el-button type="primary" @click="toSearch">搜索</el-button>
          </el-form-item>
        </el-form>
        <div class="right">
          <el-button v-if="$route.query.id" type="text" icon="el-icon-back" size="medium" @click="$router.go(-1)">返回学科</el-button>
          <el-button type="success" icon="el-icon-edit" size="small" @click="opDialog">创建标签</el-button>
        </div>
      </div>
      <!-- 提示条 -->
      <div class="promptBox">
        <i class="el-icon-info"></i> 数据一共{{total}}条
      </div>
      <!-- 表格 -->
      <el-table :data="tableList" stripe highlight-current-row style="width: 100%">
        <el-table-column type="index" label="序号" width="180" />
        <el-table-column prop="subjectName" label="所属学科" width="180" />
        <el-table-column prop="tagName" label="标签名称" />
        <el-table-column prop="username" label="创建者" />
        <el-table-column label="创建日期">
          <template #default="{row}">
            {{row.addDate | timer}}
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template #default="{row}">
            {{row.state===1?"已启用":"已禁用"}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button type="text" @click="tochangeState({id:row.id,state:row.state===1?0:1})">{{row.state===1?'禁用':'启用'}}</el-button>
            <el-button type="text" :disabled="row.state===1" @click="updataTags(row.id)">修改</el-button>
            <el-button type="text" :disabled="row.state===1" @click="delTags(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="query.page" :page-sizes="[10, 20, 30, 40]" :page-size="query.pagesize" layout="prev, pager, next,sizes, jumper" :total="total">
      </el-pagination>
    </el-card>
    <tags-add ref="tags" :showDialog="showDialog" @closeDialog="closeDialog"  />
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { list, changeState, remove } from '@/api/hmmm/tags.js'
import tagsAdd from '@/module-hmmm/components/tags-add'
export default {
  data () {
    return {
      // 渲染列表数据
      tableList: [],
      // 请求参数
      query: {
        page: 1,
        pagesize: 10,
        tabName: '',
        state: null
      },
      // 总数量
      total: 0,
      // 控制弹窗显示隐藏
      showDialog: false
    }
  },
  components: {
    // 弹窗组件
    tagsAdd
  },
  filters: {
    // 修改时间格式
    timer (val) {
      return dayjs(val).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  mounted () {
    // 获取数据
    this.getList()
    // 判断此页面是从学科过来的还是菜单过来的
    this.getShow()
  },
  methods: {
    // 获取列表信息
    async getList () {
      const { data: res } = await list(this.query)
      this.tableList = res.items
      this.total = res.counts
    },
    // 清空搜索框
    clearSearch () {
      this.query = {
        page: 1,
        pagesize: 10,
        tabName: '',
        state: null
      }
      this.getList()
    },
    // 搜索
    async toSearch () {
      const { data: res } = await list(this.query)
      this.tableList = res.items
      this.total = res.counts
    },
    // 分页器显示数量变化
    handleSizeChange (val) {
      this.query.pagesize = val
      this.getList()
    },
    // 分页器页码变化
    handleCurrentChange (val) {
      this.query.page = val
      this.getList()
    },
    // 打开弹窗
    opDialog () {
      this.showDialog = true
      if (this.$route.query.id) {
        this.$refs.tags.fromData.subjectID = Number(this.$route.query.id)
      }
    },
    // 关闭弹窗
    closeDialog () {
      this.showDialog = false
      this.getList()
    },
    // 修改标签弹窗
    updataTags (id) {
      this.showDialog = true
      this.$refs.tags.getTags(id)
    },
    // 修改标签状态
    async tochangeState ({ id, state }) {
      await changeState({ id, state })
      this.getList()
    },
    // 删除标签
    delTags (id) {
      this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        const res = await remove(id)
        if (res.status === 200) {
          this.$message.success('删除成功!')
          this.getList()
        }
      }).catch(() => {})
    },
    // 显示面包屑
    async getShow () {
      if (this.$route.query.id) {
        const { data: res } = await list({
          page: 1,
          pagesize: 10,
          subjectID: this.$route.query.id
        })
        this.tableList = res.items
        this.total = res.counts
      }
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  width: 99%;
  margin: 0 auto;
  margin-top: 10px;
}
::v-deep .is-link {
  color: #606266;
}
::v-deep .el-card__body {
  padding-top: 10px;
}
.clearfix {
  font-size: 14px;
}
.el-form {
  float: left;
  margin-left: 15px;
  .el-form-item:nth-child(2) {
    margin-left: 40px;
  }
}
.right {
  float: right;
}
.headers {
  overflow: hidden;
  margin-top: 10px;
}
.promptBox {
  background-color: #f4f4f5;
  height: 35px;
  margin-bottom: 15px;
  padding: 8px 16px;
  font-size: 13px;
  line-height: 18px;
  color: #909399;
  border-radius: 5px;
}
::v-deep.el-table th.el-table__cell.is-leaf {
  background-color: #fafafa;
  border-bottom: 2px solid #e8e8e8;
}
.el-pagination {
  margin: 10px 0;
  float: right;
}
</style>
